package cn.smvc.sys.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.smvc.common.web.BaseServlet;
import cn.smvc.sys.entity.SysDict;
import cn.smvc.sys.service.impl.SysDictServiceImpl;
import cn.smvc.task.entity.TTimedTask;

@WebServlet("/sys")
public class SysDictServlet extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3798209063088465579L;
	
	private SysDictServiceImpl sysDictService = new SysDictServiceImpl();

	public void queryPage(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		Map<String, String[]> param = request.getParameterMap();
		Map<String, Object> result = sysDictService.queryPage(param);
		
		out.println(JSON.toJSONString(result));
	}
	
	public void queryAllType(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		List<SysDict> result = sysDictService.findAllType();
		
		out.println(JSON.toJSONString(result));
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String id = request.getParameter("id");
		int result=0;
		try {
			result = sysDictService.delete(id );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		out.println(JSON.toJSONString(result));
	}
	
	public void findOne(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		String id = request.getParameter("id");
//		System.out.println(id);
		SysDict dict = sysDictService.findOne(id);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("content", dict);
		
		result .put("success", true);
		result .put("msg", "查找成功！");
		PrintWriter out = response.getWriter();
		out.println(JSON.toJSONString(result));
		
	}
	
	
}
